/**
 * External component static wrapper that composes and 'calls' external component methods
 */                                 
var Application;
var ExternalComponent =
{
   extender: null,
   
   init: function(params, extenderType)
   {
      if (typeof extenderType == "undefined")
      {
         // MSOffice mode check
		switch (true) {
		case (typeof top.window.external != "undefined" && typeof top.window.external.insertDocument != "undefined"):
			extenderType = "msoffice";
		break;
		case (parent.window.document.getElementById('xmetal') != null):
			try {
				Application = new ActiveXObject("XMetaL.Application");
			} catch (e) {
				Application = false;
			}
			extenderType = "xmetal";
		break;
		default:
			extenderType = "xopus";
		}
      }
      
      switch (extenderType.toLowerCase())
      {
         case "msoffice":
            this.extender = new MSOffice(params);
            break;
            
         case "openoffice":
            this.extender = new OpenOffice(params);
            break;
         case "xopus":
            this.extender = new Xopus(params);
            break;
	 case "xmetal":
            this.extender = new XMetaL(params);
	    break;
         default:
            alert('ExtenderType "' + extenderType + '" is not supported.');
            return;
      }
	return extenderType;
   },
   openDocument: function()
   {
      return this.extender.openDocument.apply(this.extender, arguments);
   },
   editDocument: function() 
   {
	  return this.extender.editDocument.apply(this.extender, arguments);
   },
   
   docHasExtension: function()
   {
      return this.extender.docHasExtension.apply(this.extender, arguments);
   },
   
   saveTo: function()
   {
      return this.extender.saveTo.apply(this.extender, arguments);
   },
   
   saveToAs: function()
   {
      return this.extender.saveToAs.apply(this.extender, arguments);
   },
   
   compareDocument: function()
   {
      return this.extender.compareDocument.apply(this.extender, arguments);
   },
   
   insertDocument: function()
   {
      return this.extender.insertDocument.apply(this.extender, arguments);
   },

   previewDocument: function()
   {
      return this.extender.previewDocument.apply(this.extender, arguments);
   }
}


/**
 * External component wrapper for Microsoft Internet Explorer and MSOffice add-in
 */
var MSOffice = new Class(
{
   params: {},
   
   initialize: function(params)
   {
      $extend(this.params, params);
   },

   openDocument: function(relativePath)
   {
      window.external.openDocument(relativePath);
   },
   
   docHasExtension: function(fnTrue, fnFalse)
   {
      if (window.external.docHasExtension())
      {
         fnTrue.apply(arguments.callee);
      }
      else
      {
         fnFalse.apply(arguments.callee);
      }
   },
   
   saveTo: function(relativePath)
   {
      return window.external.saveTo(relativePath);
   },
   
   saveToAs: function(relativePath, filename)
   {
      return window.external.saveToAs(relativePath, filename);
   },
   
   compareDocument: function(url)
   {
      return window.external.compareDocument(url);
   },
   editDocument : function(relativePath, nodeRef) {
	if (/ditamap$/.test(nodeRef.text)) {
		alert(nodeRef.text);
	} else {
	window.open("?r=cmis/showXopus&id=" + relativePath );
	}
   },
   insertDocument: function(relativePath, nodeRef)
   {
      relativeURL = relativePath.substring(relativePath.indexOf('?'), relativePath.length);
      return window.external.insertDocument("?r=cmis/showhtml&id=" + relativeURL, nodeRef);
   },
   previewDocument: function(relativePath, nodeRef)
   {
	window.open("?r=cmis/showhtml&id=" + relativePath );
   }
});


/**
 * External component wrapper for XMetaL
 */
var XMetaL = new Class(
{
   params: {},

   initialize: function(params)
   {
      $extend(this.params, params);
   },

   openDocument: function(relativePath)
   {
      window.external.openDocument(relativePath);
   },

   docHasExtension: function(fnTrue, fnFalse)
   {
      if (window.external.docHasExtension())
      {
         fnTrue.apply(arguments.callee);
      }
      else
      {
         fnFalse.apply(arguments.callee);
      }
   },
   saveTo: function(relativePath)
   {
      return window.external.saveTo(relativePath);
   },

   saveToAs: function(relativePath, filename)
   {
      return window.external.saveToAs(relativePath, filename);
   },

   compareDocument: function(url)
   {
      return window.external.compareDocument(url);
   },
   editDocument : function(relativePath, nodeRef) {
   },
   insertDocument: function(relativePath, nodeRef)
   {
                /*pathItems = nodeRef.getPath().split(">");
                fileRoot = pathItems[pathItems.length - 2];
                filepath = fileRoot + "/" + nodeRef.text;
                filepath = filepath.replace(/\//g,"\\");
                filepath = mappedDrive + filepath;
                Application.Documents.Open(filepath);
                */
		Application.Documents.Open(window.location + "/?r=cmis/show&id=" + relativePath);
		
		return false;

   },
   previewDocument: function(relativePath, nodeRef)
   {
   }
});


/**
 * External component wrapper for XOpus
 */
var Xopus = new Class(
{
   params: {},
   
   initialize: function(params)
   {
      $extend(this.params, params);
   },

   openDocument: function(relativePath)
   {
      window.external.openDocument(relativePath);
   },
   
   docHasExtension: function(fnTrue, fnFalse)
   {
      if (window.external.docHasExtension())
      {
         fnTrue.apply(arguments.callee);
      }
      else
      {
         fnFalse.apply(arguments.callee);
      }
   },
   
   saveTo: function(relativePath)
   {
      return window.external.saveTo(relativePath);
   },
   
   saveToAs: function(relativePath, filename)
   {
      return window.external.saveToAs(relativePath, filename);
   },
   
   compareDocument: function(url)
   {
      return window.external.compareDocument(url);
   },
   editDocument : openResource,
   insertDocument: openResource,
   previewDocument: function(relativePath, nodeRef)
   {
	Ext.getCmp('tabPanel').add([{
	closable: true,
	title: nodeRef.text,
	autoScroll: true,
	html:"<iframe src='?r=cmis/showhtml&id=" + relativePath + "' width='100%' height='100%' border='0' style='border:0px'/>"
	}]);
        Ext.getCmp('tabPanel').activate(Ext.getCmp('tabPanel').items.length - 1);
   }
});

/**
 * External component wrapper for XOpus
 */
var Lookup = new Class(
{
   params: {},
   
   initialize: function(params)
   {
      $extend(this.params, params);
   },

   openDocument: function(relativePath)
   {
      window.external.openDocument(relativePath);
   },
   
   docHasExtension: function(fnTrue, fnFalse)
   {
      if (window.external.docHasExtension())
      {
         fnTrue.apply(arguments.callee);
      }
      else
      {
         fnFalse.apply(arguments.callee);
      }
   },
   
   saveTo: function(relativePath)
   {
      return window.external.saveTo(relativePath);
   },
   
   saveToAs: function(relativePath, filename)
   {
      return window.external.saveToAs(relativePath, filename);
   },
   
   compareDocument: function(url)
   {
      return window.external.compareDocument(url);
   },
   editDocument : function(relativePath, nodeRef) {
		url = "?r=cmis/showXopus&id=" + relativePath;
		Ext.getCmp('tabPanel').add([{
	    	closable: true,
	    	title: nodeRef.text,
	    	autoScroll: true,
	    	html:"<iframe src='" + url + "' width='100%' height='100%' border='0' style='border:0px'/>"
		}]);
		Ext.getCmp('tabPanel').activate(Ext.getCmp('tabPanel').items.length - 1);
   },
   insertDocument: function(relativePath, nodeRef)
   {
		alert('x');
			return "X";
   },
   previewDocument: function(relativePath, nodeRef)
   {
	Ext.getCmp('tabPanel').add([{
	closable: true,
	title: nodeRef.text,
	autoScroll: true,
	html:"<iframe src='?r=cmis/showhtml&id=" + relativePath + "' width='100%' height='100%' border='0' style='border:0px'/>"
	}]);
        Ext.getCmp('tabPanel').activate(Ext.getCmp('tabPanel').items.length - 1);
   }
});


/**
 * Common EXT Functions
 */

/**
 * Open resource for editing from stand-alone mode
 */

function openResource(relativePath, nodeRef) {
        if (/ditamap$/.test(nodeRef.text)) {
		Ext.Msg.alert("Notice","The Map editor feature is currently in its early development phase. Play nicely ;)");
                Ext.getCmp('tabPanel').add([{
                        xtype: 'treepanel',
                        tools: [{
                                        id:'minus',
					handler: function(ev, toolEl, panel, tc) {
                                                        panel.collapseAll();
                                        }
                                },{
                                        id:'plus',
                                        handler: function(ev, toolEl, panel, tc) {
                                                        panel.expandAll();
                                        }
                                },{
                                        id:'refresh',
                                        handler: function(ev, toolEl, panel, tc) {
                                                        panel.getRootNode().reload();
                                        }
                                },{
                                        id:"save",
                                        handler: function(ev, toolEl, panel, tc) {
                                                myTree = new Ext.tree.XmlTreeSerializer(panel);
                                                alert(myTree.toString());

                                        }
                                }],
                        title: nodeRef.text,
                        closable: true,
                        width: 'auto',
                        useArrows: true,
                        animate: true,
                        enableDD: true,
                        containerScroll: true,
                        border: false,
                        dataUrl: '?r=cmis/getMapNodes',
                        requestMethod: 'GET',
                        rootVisible: false,
                        root: {
                                nodeType: 'async',
                                allowDrop:false,
                                text: 'CMIS',
                                draggable: false,
                                id: nodeRef.attributes['objectId']
                        },
                        listeners:{
				beforenodedrop: function(dropEvent) {
					Ext.Msg.alert("Notice","The item dragged will be appended to this map, but should not be removed from the source");
					
					return true;
				}
                        },
                        contextmenu: new Ext.menu.Menu({
                                items:[{
                                text:'delete'
                                }]
                        })
        	}]);
               Ext.getCmp('tabPanel').activate(Ext.getCmp('tabPanel').items.length - 1);
        } else {
                url = "?r=cmis/showXopus&id=" + relativePath;
                Ext.getCmp('tabPanel').add([{
                closable: true,
                title: nodeRef.text,
                autoScroll: true,
                html:"<iframe src='" + url + "' width='100%' height='100%' border='0' style='border:0px'/>"
                }]);
                Ext.getCmp('tabPanel').activate(Ext.getCmp('tabPanel').items.length - 1);
        }
}
